import request from '@/utils/request'
import { randomId, json2FormData } from '@/utils'
import memCache from '@/utils/memCache'

// 分页查询
export function getRealityPage(data) {
  return request({
    url: '/app/v1/scheme/2.0/realityPage',
    method: 'post',
    data: json2FormData(data)
  })
}

// 详情
export function getRealityById(id) {
  return request({
    method: 'GET',
    url: `/app/v1/scheme/2.0/getRealityById`,
    params: { id }
  })
}

// 查找筛选条件（风格、空间）
export function getAttrList() {
  return request({
    method: 'GET',
    url: '/app/v1/scheme/2.0/attribute'
  }).then(res => {
    // 处理数据结构，符合NavFilter组件
    const result = []
    // 风格 adsStyleDTOS
    result.push({
      title: '风格',
      flag: 'adsStyleDTOS',
      unId: randomId(),
      show: false,
      children: (res.data.adsStyleDTOS || []).map((e, i) => {
        e.label = e.styleName
        e.value = e.code
        e.index = i
        return e
      })
    })
    // 空间 adsSpaceDTOS
    result.push({
      title: '空间',
      flag: 'adsSpaceDTOS',
      unId: randomId(),
      show: false,
      children: (res.data.adsSpaceDTOS || []).map((e, i) => {
        e.label = e.spaceName
        e.value = e.id
        e.index = i
        return e
      })
    })
    return {
      rawDdata: res.data,
      data: result
    }
  })
}

export const getRealitySceneFilter = memCache('getRealitySceneFilter', getAttrList)

/**
 * 根据风格查询系列
 * @param {string} belongStyleId 风格ID
*/
export function getSeriesByStyleId(belongStyleId) {
  return request({
    method: 'POST',
    url: '/app/v1/scheme/2.0/seriesPage',
    data: { belongStyleId, seriesType: 0 }
  }).then(res => {
    // 处理数据结构，符合NavFilter组件
    const result = []
    // 系列 adsSeriesDTOS
    result.push({
      title: '系列',
      flag: 'adsSeriesDTOS',
      unId: randomId(),
      multiple: false,
      isMultChoose: false,
      show: false,
      children: (res.data || []).map((e, i) => {
        e.label = e.seriesName
        e.idx = e.seriesCode
        // e.value = e.idx
        e.index = i
        return e
      })
    })
    return {
      rawDdata: res.data,
      data: result
    }
  })
}

/**
 * 根据空间查品类
 * @param {string} spaceCode 空间编码
*/
export function getCategoryByOneSpace(spaceCode) {
  return request({
    method: 'GET',
    url: '/app/v1/scheme/2.0/getCategoryByOneSpace',
    params: { spaceCode }
  }).then(res => {
    // 处理数据结构，符合NavFilter组件
    const result = []
    // 品类 adsCategoryDTOS
    result.push({
      title: '品类',
      flag: 'adsCategoryDTOS',
      unId: randomId(),
      show: false,
      children: (res.data || []).map((e, i) => {
        e.label = e.categoryName
        e.idx = e.categoryCode
        // e.value = e.idx
        e.index = i
        return e
      })
    })
    return {
      rawDdata: res.data,
      data: result
    }
  })
}
